
//获取渠道数据
function getChannelList() {
    var begin_date,end_date,params;
    begin_date = $('#datepicker_begin').val();
    end_date = $('#datepicker_end').val();
    params = {
        'begin_date':begin_date,
        'end_date' :end_date
    }
    var list = {};
    $.ajax({
        url:channel_url,
        type:'post',
        async:false,
        dataType:'json',
        data:params,
        success:function(data) {
            list = data;
        }
    });
    return list;
}
//分日 - 获取7日数据
function getCountList() {
    var begin_date,end_date,params;
    begin_date = $('#datepicker_begin').val();
    end_date = $('#datepicker_end').val();
    params = {
        'begin_date':begin_date,
        'end_date' :end_date
    }
    var list = {};
    $.ajax({
        url:count_url,
        type:'post',
        async:false,
        dataType:'json',
        data:params,
        success:function(data) {
            list = data;
        }
    });
    return list;
}
//填充 分日 表格
function fillDayTableList(lists) {
    $('#day_list tbody').html("");
    var content = "";
    var keys = ['install','active','uninstall','installun'];
    var line = 0;
    for(var item in lists) {
        content += "<tr>";
        content += "<td>"+item.substr(1,4)+"-"+item.substr(5,2)+"-"+item.substr(7,2)+"</td>";
        for(var key in keys) {
            var currentkey = keys[key];
            if(currentkey in lists[item]) {
                content += "<td>"+lists[item][currentkey] +"</td>";
            } else {
                content += "<td>0</td>";
            }
        }
        content += "</tr>";
        line ++;
    }
    if(content == "") {
        $("#day_list tbody").html("<tr><td colspan='5'>没有数据！</td></tr>");
        $("#dcount").html(line);
    } else {
        $("#day_list tbody").html(content);
        $("#dcount").html(line);
    }
}
//分日 排序
$('#day_list thead th').click(function() {
    $('#day_list thead th').find('span').text('');
    var index = $(this).attr('arc');
    var type = $(this).find('span').attr('class');
    if(type == 'up'|| type == 'up1') {
        fillDayTableList(sortArray(dayList, index, 'desc'));
        $(this).find('span').attr('class','down');
    } else {
        fillDayTableList(sortArray(dayList, index, 'asc'));
        $(this).find('span').attr('class','up1');
    }
});
//填充分渠道表格
function fillChannelTableList(lists) {
    var begin_date,end_date;
    begin_date = $('#datepicker_begin').val();
    end_date = $('#datepicker_end').val();

    $('#channel_list tbody').html("");
    var content = "";
    var keys = ['install','active','uninstall','installun'];
    var line = 0;
    for(var item in lists) {
        content += "<tr>";
        content += "<td><a href='/Home/Install/channeldetail?channel_id="+item.substr(1,item.length - 2)+"&bdate="+begin_date+"&edate="+end_date+"'>"+item.substr(1,item.length - 2)+"</a></td>";
        for(var key in keys) {
            var currentkey = keys[key];
            if(currentkey in lists[item]) {
                content += "<td>"+lists[item][currentkey] +"</td>";
            } else {
                content += "<td>0</td>";
            }
        }
        content += "</tr>";
        line++;
    }
    if(content == "") {
        $("#channel_list tbody").html("<tr><td colspan='5'>没有数据！</td></tr>");
        $("#dcount").html(line);
    } else {
        $("#channel_list tbody").html(content);
        $("#dcount").html(line);
    }
}
//分日 图标
function drawDayLine() {
    var typename = new Array();
        typename['install'] = '安装量';
        typename['uninstall'] = '卸载量';
        typename['active'] = '日活';
        typename['installun'] = '当日安装即卸载';

    var params = {
        xType:[],
        title:'',
        subtitle:'',
        valuelist:[],
        legend: {
            selectedMode:'multiple',
            show:true,
            data: ["安装量","卸载量","日活","当日安装即卸载"],
            selected:{
                "日活":false,
                "当日安装即卸载":false
            }
        }
    };
    for(var type in typename) {
        var item = {
            name: typename[type],
            type: 'line',
            smooth: true,
            itemStyle: {normal: {areaStyle: {type: 'default'}}},
            data:[]
        };
        for (var key in dayList) {
            if(typeof dayList[key][type] != 'undefined') {
                item.data.push(parseInt(dayList[key][type]));
            } else {
                item.data.push(0);
            }
        }
        params.valuelist.push(item);
    }

    for (var key in dayList) {
        var date = key.substr(0,4)+'-'+key.substr(4,2)+'-'+key.substr(6,2);
        params.xType.push(date);
    }
    drawline('chart_day',params);
}
function drawChannelLine(lists){
    // 组装折线图数据
    var xType = [];
    var series = [],result = [];
    var ntype = ['安装','卸载','日活','当日安装即卸载'];
    var dtype = ['install','uninstall','active','installun'];
    for(var i=0;i<dtype.length;i++){
        series[dtype[i]] = {
            name  : ntype[i],
            type  : 'line',
            smooth:true,
            itemStyle:{normal:{areaStyle: {type: 'default'}}},
            data:[]
        };
    }
    for(var cdata in lists){
        for(var sdkey in series){
            var _tdata = (lists[cdata][sdkey])?lists[cdata][sdkey]:0;
            series[sdkey].data.push(_tdata);
        }
        xType.push(cdata);
    }
    for(var _eseries in series){
        result.push(series[_eseries]);
    }
    var lineData = {
        title : '分渠道统计',
        subtitle:'',
        legend:{
            selectedMode:'single',
            show:true,
            data:['安装' ,'卸载','日活','当日安装即卸载'],
            selected:{
                '卸载':false,
                '日活':false,
                '当日安装即卸载':false
            }
        },
        xType:xType,
        valuelist:result
    };
    drawline('channel',lineData);
}
// 渠道 -- 图标
$(".channel").click(function() {
    currpage = "channel";
    var lists = getChannelList();

    //排序
    $('#channel_list thead th').click(function() {
        $('#channel_list thead th').find('span').text('');
        var index = $(this).attr('arc');
        var type = $(this).find('span').attr('class');
        if(type == 'up'|| type == 'up1') {
            fillChannelTableList(sortArray(lists, index, 'desc'));
            $(this).find('span').attr('class','down');
        } else {
            fillChannelTableList(sortArray(lists, index, 'asc'));
            $(this).find('span').attr('class','up1');
        }
    });
    drawChannelLine(lists);
    fillChannelTableList(sortArray(lists, 'pkey', 'asc'));
});
$('.day').click(function(){
    currpage = "day";
    Page.init();
});
// 获取总量数据
function getAllResult(params) {
    var result = {};
    $.ajax({
        url:all_url,
        type:'post',
        async:false,
        dataType:'json',
        data:params,
        success:function(data) {
            result = data;
        }
    });
    return result;
}
//展示总量数据
function showAllData(){
    var a_data = getAllResult();
    $('#allData dd').eq(0).html(a_data.install) ;
    // $('#allData dd').eq(1).html(a_data.active) ;
    $('#allData dd').eq(1).html(a_data.uninstall) ;
    $('#allData dd').eq(2).html(a_data.installun) ;
}
function showDate(){
    daystr = getDateWeek();
    $(".showdate").html(daystr);
}
var dayList;
var daystr;
var currpage;
var Page = {
    init:function() {
        currpage = "day";
        showDate();
        // 日历显示
        $( "#datepicker_begin" ).datepicker({
            dateFormat: 'yy-mm-dd',
            maxDate: new Date(),
            minDate:'2016-06-03',
            showOtherMonths: true,
            selectOtherMonths: true,
            onClose: function( selectedDate ) {
                $( "#datepicker_end" ).datepicker( "option", "minDate", selectedDate );
            }
        });
        $( "#datepicker_end" ).datepicker({
            dateFormat: 'yy-mm-dd',
            maxDate: new Date(),
            minDate:'2016-06-03',
            showOtherMonths: true,
            selectOtherMonths: true,
            onClose: function( selectedDate ) {
                $( "#datepicker_begin" ).datepicker( "option", "maxDate", selectedDate );
            }
        });
        showAllData();
        dayList = getCountList();
        drawDayLine();
        fillDayTableList(sortArray(dayList, 'pkey', 'asc'));
    },
    resize:function() {
        drawDayLine();
    }
};
Page.init();
$(window).resize(function () {
    Page.resize();
});
$("#date_dur input").change(function(){
    changeData(currpage);
});
function changeData(cur_page){
    showDate();
    if(cur_page =='day'){
        dayList = getCountList();
        drawDayLine();
        fillDayTableList(sortArray(dayList, 'pkey', 'asc'));
    }else{
        d_lists = getChannelList();
        drawChannelLine(d_lists);
        fillChannelTableList(sortArray(d_lists, 'pkey', 'asc'));
    }
}